package main

import (
	"fmt"
	"sync"
	"time"
)

var lockCount = 0

var mutex sync.Mutex

// 并发 累加 count count++ 不是原子操作
func concurrentTest() {
	for i := 0; i < 300; i++ {
		go compute()
	}
	time.Sleep(time.Second * 3)
	fmt.Println(" concurrentTest count >>>> ", lockCount)
}

// 并发安全的测试
func concurrentSafeTest() {
	for i := 0; i < 100; i++ {
		go computeSafe()
	}
	time.Sleep(time.Second * 3)
	fmt.Println("concurrentSafeTest count >>>> ", lockCount)
}

func compute() {
	lockCount++
}

func computeSafe() {
	mutex.Lock()
	lockCount++
	mutex.Unlock()
}

func main() {
	concurrentTest()
	lockCount = 0
	concurrentSafeTest()
}
